<html>
<head><meta charset="utf-8"><title>Arrays are always contiguous, right? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html">Arrays are always contiguous, right?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209419522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209419522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tony Arcieri <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209419522">(Sep 08 2020 at 17:39)</a>:</h4>
<p>#lazy sanity check on something I'm pretty sure is correct...but this is a cryptographic context and I'm always paranoid about that.</p>
<p><code>[[u8; 16]; 4]</code> can be safely cast to <code>[u8; 64]</code>, correct?</p>



<a name="209419678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209419678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209419678">(Sep 08 2020 at 17:40)</a>:</h4>
<p>I believe so, yes, or at least I've seen discussion of the more general <code>&amp;[[T; N]]</code> -&gt; <code>&amp;[T]</code> and vice versa</p>



<a name="209419709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209419709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209419709">(Sep 08 2020 at 17:40)</a>:</h4>
<p>Yes, array layout is fully guaranteed, stride=size always, and the same element type means no trickiness.</p>
<p>(I look forward to safe transmute so that these questions will be answered by the type checker <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span>)</p>



<a name="209419712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209419712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tony Arcieri <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209419712">(Sep 08 2020 at 17:40)</a>:</h4>
<p>safe reference conversions for those would be amazing</p>



<a name="209419731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209419731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tony Arcieri <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209419731">(Sep 08 2020 at 17:41)</a>:</h4>
<p>aah yes this seems like a "safe transmute" thing</p>



<a name="209424444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209424444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209424444">(Sep 08 2020 at 18:19)</a>:</h4>
<p><code>bytemuck</code> crate can do this while we wait for safe casting to become part of core</p>



<a name="209755746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209755746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209755746">(Sep 11 2020 at 08:59)</a>:</h4>
<blockquote>
<p>stride=size always</p>
</blockquote>
<p>Yeah... I dont think we actually document that there'd be some amazing benefits from being able to make the size not a multiple of the alignment... but it is probably too late :/</p>



<a name="209755801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209755801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209755801">(Sep 11 2020 at 09:00)</a>:</h4>
<p>maybe it is time some RFCs or FCPs this for good so the UCG can close that chapter</p>



<a name="209755818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209755818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209755818">(Sep 11 2020 at 09:00)</a>:</h4>
<p>see <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/176">https://github.com/rust-lang/unsafe-code-guidelines/issues/176</a></p>



<a name="209756027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209756027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209756027">(Sep 11 2020 at 09:02)</a>:</h4>
<p>Quite unfortunate that size_of already says it gives the array stride, as the stride could also be retrieved from <code>size_of::&lt;[T; 1]&gt;</code> which is independently specified as being <code>repr(c)</code> already thus giving the intended stride.</p>



<a name="209815939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209815939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209815939">(Sep 11 2020 at 18:13)</a>:</h4>
<p>I can't help but feel like there's a fundamental mindset issue here.</p>



<a name="209816007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209816007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209816007">(Sep 11 2020 at 18:14)</a>:</h4>
<p>Like, the question is not "are there benefits we could get by not guaranteeing this", it's "how painful would it be to not guarantee this".</p>



<a name="209816113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209816113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209816113">(Sep 11 2020 at 18:14)</a>:</h4>
<p>(in this particular case, I'd say that that ship sailed long before Rust existed)</p>



<a name="209816519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209816519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209816519">(Sep 11 2020 at 18:18)</a>:</h4>
<p>We're not starting from "we guarantee nothing" and adding to that; we're starting from "what do programmers expect, from intuition, other environments, and how hardware actually works", and any non-guarantee <em>takes away</em> from that. And we can absolutely do that, when it's important. But we shouldn't just treat it as "should we guarantee that or not"; it shouldn't start from neutral.</p>



<a name="209816593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209816593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209816593">(Sep 11 2020 at 18:19)</a>:</h4>
<p>Yeah, I don't care about edge case optimizations. They just don't matter. Make the normal case make sense first, then maybe do something as long as you don't break that first ideal.</p>



<a name="209828964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209828964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209828964">(Sep 11 2020 at 20:06)</a>:</h4>
<p>If I cared stronger about this I'd gather some data on whether there'd be significant space saving from not having to add padding to the end of each type to make size a multiple of alignment... I hear Swift does that (not add padding) so I guess I'm just a bit envious that they get more layout optimizations than we do^^</p>



<a name="209829145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209829145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209829145">(Sep 11 2020 at 20:08)</a>:</h4>
<p>In this case I think it really comes down to what programmers expect from other environments -- it's hard to do anything that C didn't do. So frustrating that decisions made 50 years ago are still holding us back...</p>



<a name="209829279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209829279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209829279">(Sep 11 2020 at 20:10)</a>:</h4>
<p>At the same time the UCG doesn't have the power to decide that Rust will never do this, so it would take something more official than that so settle the issue for good.</p>



<a name="209834881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209834881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209834881">(Sep 11 2020 at 21:02)</a>:</h4>
<p>It might be possible to have an opt-in mechanism for such optimizations, for code that knows they're OK.</p>



<a name="209834965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209834965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209834965">(Sep 11 2020 at 21:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F/near/209828964">said</a>:</p>
<blockquote>
<p>If I cared stronger about this I'd gather some data on whether there'd be significant space saving from not having to add padding to the end of each type to make size a multiple of alignment... I hear Swift does that (not add padding) so I guess I'm just a bit envious that they get more layout optimizations than we do^^</p>
</blockquote>
<p>I'd be curious about that. I wouldn't think it'd be huge. You'd need a lot of times of the form <code>(u64, u8)</code> for that to make a difference.</p>



<a name="209835036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209835036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209835036">(Sep 11 2020 at 21:04)</a>:</h4>
<p>You'd also need your memory allocator to not just hand you 16 bytes for that anyway.</p>



<a name="209836235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209836235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209836235">(Sep 11 2020 at 21:17)</a>:</h4>
<p>it could be stack tho'</p>



<a name="209836377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209836377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209836377">(Sep 11 2020 at 21:18)</a>:</h4>
<p>Myself, I was until basically this week fairly naive to the details of alignment, so I would have expected stride == packed size.</p>



<a name="209837117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209837117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209837117">(Sep 11 2020 at 21:26)</a>:</h4>
<p><span class="user-mention" data-user-id="281757">@Jubilee</span> For stack it's worse, because you need to keep the stack aligned for <em>other</em> things you might want to add to it. Plus, the compiler would have to <em>avoid</em> generating code that assumes padding on the end.</p>



<a name="209837130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209837130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209837130">(Sep 11 2020 at 21:26)</a>:</h4>
<p>(because that padding might have something in it).</p>



<a name="209837630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209837630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209837630">(Sep 11 2020 at 21:31)</a>:</h4>
<p>oof. good point.</p>



<a name="209846887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209846887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209846887">(Sep 11 2020 at 23:28)</a>:</h4>
<p>I wish we had a flag for struct fields that caused <code>mystruct.field</code> to be treated as an rvalue - i.e., no references, but if the type is Copy, <code>&amp;</code> can implicitly make a copy on the stack and take a reference to that</p>



<a name="209846901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209846901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209846901">(Sep 11 2020 at 23:28)</a>:</h4>
<p>and then the compiler could go wild with layout optimizations</p>



<a name="209847057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209847057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209847057">(Sep 11 2020 at 23:31)</a>:</h4>
<p>but</p>
<blockquote>
<p>Josh Triplett: You'd also need your memory allocator to not just hand you 16 bytes for that anyway.</p>
</blockquote>
<p>even if the allocator aligns, it's still suboptimal with things like <code>((u64, u8), u8)</code></p>



<a name="209852272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209852272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209852272">(Sep 12 2020 at 01:17)</a>:</h4>
<p>+1, <span class="user-mention" data-user-id="198590">@comex</span> -- that's even a gateway to bitfields.</p>



<a name="209862827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209862827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209862827">(Sep 12 2020 at 03:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="198590">comex</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F/near/209847057">said</a>:</p>
<blockquote>
<p>but</p>
<blockquote>
<p>Josh Triplett: You'd also need your memory allocator to not just hand you 16 bytes for that anyway.</p>
</blockquote>
<p>even if the allocator aligns, it's still suboptimal with things like <code>((u64, u8), u8)</code></p>
</blockquote>
<p>Now that one I'd agree with re-layout of, as long as it's <code>#[repr(Rust)]</code>.</p>



<a name="209862871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209862871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209862871">(Sep 12 2020 at 03:54)</a>:</h4>
<p>I know there's a general rule about references to inner objects, but it seems like that one could be made to satisfy the "as-if" rule.</p>



<a name="209863389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863389">(Sep 12 2020 at 04:12)</a>:</h4>
<p>Well we already let people opt in to an explicitly C-like ABI in many cases, the question is often "how C-like should our ABI be by default?" Which now comes down to: in what cases is it more annoying to have to annotate for Rusty optimizations vs. more annoying to have to annotate for C-like behaviors?</p>



<a name="209863455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863455">(Sep 12 2020 at 04:14)</a>:</h4>
<p>Would there be a way to assume <code>#[Repr(Rust)]</code> by default except when something is exposed via a C API? Or would that be a hornet's nest?</p>



<a name="209863473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863473">(Sep 12 2020 at 04:15)</a>:</h4>
<p>And the tuple-within-tuple thing with the u8, u64, u8, might be expected to... well, I mentioned I didn't know what alignment really meant until recently but I would expect it to take on a 128 bit alignment and be 80 bits wide inside that.</p>



<a name="209863533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863533">(Sep 12 2020 at 04:17)</a>:</h4>
<p>Would that be consistent with C's struct-inside-struct?</p>



<a name="209863538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863538">(Sep 12 2020 at 04:17)</a>:</h4>
<p>Probably not.<br>
But I mean, it'd make sense for Rust.</p>



<a name="209863582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863582">(Sep 12 2020 at 04:19)</a>:</h4>
<p>Unless alignment has proven performance benefits for modern CPUs, it's just wasting memory, no?</p>



<a name="209863694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863694">(Sep 12 2020 at 04:22)</a>:</h4>
<p>Alignment makes a _lot_ of sense _within_ a 2^N bitfield, and is very performance related there, but inter-field alignment for a given struct... I believe it's a bit more "eh" there?</p>



<a name="209863756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863756">(Sep 12 2020 at 04:24)</a>:</h4>
<p>Like, if given a u64, u8, u8, in any sequence, the best way to handle that is to shove the u64 in one half of a 128 bit aligned struct and the u8s in the other half, pretty sure?</p>
<p>But like, if you do SIMD that isn't 16 byte aligned you basically are shooting your performance for no particular reason, as far as I understand it, if it is even allowed at all on a given architecture/OS combination.</p>



<a name="209863773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863773">(Sep 12 2020 at 04:25)</a>:</h4>
<p>You raise a good point. But is SIMD even applicable to structs?</p>



<a name="209863854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863854">(Sep 12 2020 at 04:27)</a>:</h4>
<p>(I guess it could be, I was thinking more of the struct-array case rather than array-in-a-struct).</p>



<a name="209863905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863905">(Sep 12 2020 at 04:28)</a>:</h4>
<p>Should I share the Good News of the the extremely common Struct-of-Arrays pattern in Rust...? <span aria-label="eyes" class="emoji emoji-1f440" role="img" title="eyes">:eyes:</span></p>



<a name="209863977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209863977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209863977">(Sep 12 2020 at 04:30)</a>:</h4>
<p>Not sure if sarcastic. <code>Fry.jpg</code></p>



<a name="209864051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209864051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209864051">(Sep 12 2020 at 04:33)</a>:</h4>
<p>I am quite serious! <a href="https://github.com/termhn/ultraviolet">https://github.com/termhn/ultraviolet</a></p>



<a name="209864148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209864148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209864148">(Sep 12 2020 at 04:36)</a>:</h4>
<p>Oh. I was thinking it would be a non-idiomatic way to code, but of course, it makes sense with the right abstractions, as always. <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="209864161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209864161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209864161">(Sep 12 2020 at 04:37)</a>:</h4>
<p>there are _many_ users of struct of array patterns and libraries in Rust. I do not think it is "non-idiomatic". In fact, it is often the opposite for Rust, since it can significantly reduce ownership problems. But then, I suppose we're just about off topic now.</p>



<a name="209864280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209864280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209864280">(Sep 12 2020 at 04:41)</a>:</h4>
<p>Getting back to the topic... I guess alignment makes sense, but only for arrays?</p>



<a name="209870490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209870490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jannis Harder <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209870490">(Sep 12 2020 at 08:10)</a>:</h4>
<p>Another benefit of alignment is that aligned values that fit into a cache line will never cross a cache line boundary, which can be faster when doing random accesses (loading 1 instead of 2 cache lines) and is a requirement for doing any kind of atomic operation</p>



<a name="209870925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209870925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209870925">(Sep 12 2020 at 08:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F/near/209835036">said</a>:</p>
<blockquote>
<p>You'd also need your memory allocator to not just hand you 16 bytes for that anyway.</p>
</blockquote>
<p>no, even if the allocator does that, sticking a <code>T = (u64, u8)</code> into a <code>(T, u8)</code> wastes a lot of space. (Ah, that point was already made.)</p>
<p>In Miri I think we'd like to have a type that's basically <code>(u128, u8)</code> and losing those 15 bytes really hurts when we then have an enum where this is one of the variants -- the enum disrciminant adds another 8 bytes :( . Right now <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/interpret/enum.Scalar.html">the type is embedded in the enum that surrounds it</a> partially for this reason.<br>
With enough hacking we could maybe make it <code>([u8; 16], u8)</code> but that would be rather painful.</p>



<a name="209871046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209871046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209871046">(Sep 12 2020 at 08:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F/near/209862871">said</a>:</p>
<blockquote>
<p>I know there's a general rule about references to inner objects, but it seems like that one could be made to satisfy the "as-if" rule.</p>
</blockquote>
<p>I don't see how? You can take references to arbitrary subobjects currently, so unless you want data layout to depend on a whole-program analysis that determines which subobjects actually have references taken to them (which I think is a bad idea for several reasons), this needs opt-in.<br>
Also, for the Miri case above we either do need references to that <code>(u128,u8)</code> or we pass it around by value everywhere (we don't need mutable references), but then if we pass by value I'm a bit worried that this is introducing too much copying? maybe that's not a problem though.</p>



<a name="209893763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209893763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209893763">(Sep 12 2020 at 18:27)</a>:</h4>
<p>The enum you mentioned seems like something we ought to be able to improve with better niche support.</p>



<a name="209893933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209893933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209893933">(Sep 12 2020 at 18:30)</a>:</h4>
<p>Regarding the as-if approach: it seems like we ought to be able to say that <code>(u64, u8)</code> has size 16, and <code>((u64, u8), u8)</code> also has size 16.</p>



<a name="209896719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209896719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209896719">(Sep 12 2020 at 19:41)</a>:</h4>
<p>I could write reasonably <code>unsafe</code> code today, though, that writes 16 bytes into a <code>&amp;mut (u64, u8)</code> (not hard-coded, just because that's what sizeof says), and doing that will fundamentally overwrite whatever that third u8 contained, if the type isn't bigger by adding it.  (Like <code>ptr::write_bytes(that_ptr, 0_u8, 1)</code> seems quite reasonable today -- and it's defined to "Set <code>count * size_of::&lt;T&gt;()</code> bytes of memory" so maybe it went through a helper than ended up calling <code>write_bytes(that_ptr as *mut u8, 0_u8, sizeof((u64, u8)))</code>...)</p>



<a name="209912999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209912999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209912999">(Sep 13 2020 at 04:20)</a>:</h4>
<p>(Also applies to ptr::copy / memcpy.)</p>



<a name="209925763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209925763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209925763">(Sep 13 2020 at 11:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F/near/209893933">said</a>:</p>
<blockquote>
<p>Regarding the as-if approach: it seems like we ought to be able to say that <code>(u64, u8)</code> has size 16, and <code>((u64, u8), u8)</code> also has size 16.</p>
</blockquote>
<p>I don't think we can -- to name one more example, <code>mem::swap</code> on <code>&amp;mut (u64, u8)</code> would then clobber the 2nd field of that <code>((u64, u8), u8)</code>.</p>



<a name="209925836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays%20are%20always%20contiguous%2C%20right%3F/near/209925836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F.html#209925836">(Sep 13 2020 at 11:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Arrays.20are.20always.20contiguous.2C.20right.3F/near/209893763">said</a>:</p>
<blockquote>
<p>The enum you mentioned seems like something we ought to be able to improve with better niche support.</p>
</blockquote>
<p>again, we cannot -- at least, not the way padding works right now. if we adopted "guaranteed-zero padding", thenw e could do more enum niche optimizations. but that still would not help with <code>((u64, u8), u8)</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>